package com.mall.ibatis.persistence.sqlmapdao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.dao.client.DaoManager;
import com.mall.ibatis.bean.Goods;
import com.mall.ibatis.bean.JqgridParams;
import com.mall.ibatis.persistence.iface.GoodsDao;

public class GoodsSqlMapDao extends BaseSqlMapDao implements GoodsDao {

	public GoodsSqlMapDao(DaoManager daoManager) {
		super(daoManager);
	}

	@SuppressWarnings("unchecked")
	public List<Goods> showGoods(JqgridParams params) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sidx", params.getSidx());
		map.put("sord", params.getSord());
		map.put("criterion", params.getCriterion());
		return queryForList("showGoods", map);
	}

	public int showGoodsCount(JqgridParams params) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sidx", params.getSidx());
		map.put("sord", params.getSord());
		map.put("criterion", params.getCriterion());
		return (Integer)queryForObject("showGoodsCount", map);
	}

	public int addgs(Goods gs) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("GOODS_NAME", gs.getGOODS_NAME());
		map.put("GOODS_SN", gs.getGOODS_SN());
		map.put("GOODS_COMMENT", gs.getGOODS_COMMENT());
		map.put("GOODS_ACTIVE", gs.getGOODS_ACTIVE());
		map.put("GOODS_TYPE", gs.getGOODS_TYPE());
		map.put("GOODS_BRAND", gs.getGOODS_BRAND());
		map.put("GOODS_SHELVE", gs.getGOODS_SHELVE());
		map.put("GOODS_PRICE", gs.getGOODS_PRICE());
		map.put("GOODS_ARRIVE_DATE", gs.getGOODS_ARRIVE_DATE());
		map.put("GOODS_BAR_CODE", gs.getGOODS_BAR_CODE());
		map.put("GOODS_ORDER", gs.getGOODS_ORDER());
		map.put("GOODS_IMAGE", gs.getGOODS_IMAGE());
		return update("addgs",map);
	}

	public String deleteGoods(String rowID) throws SQLException {
		int i = delete("deleteGoods",rowID);
		if(i>0){
			return "删除成功";
		}
		return "删除失败";
	}

	public String saveGoods(List<Goods> gsList) {
		try {
			daoManager.startTransaction();
			for(Goods gs:gsList){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("GOODS_ID", gs.getGOODS_ID());
				map.put("GOODS_NAME", gs.getGOODS_NAME());
				map.put("GOODS_SN", gs.getGOODS_SN());
				map.put("GOODS_COMMENT", gs.getGOODS_COMMENT());
				map.put("GOODS_ACTIVE", gs.getGOODS_ACTIVE());
				map.put("GOODS_TYPE", gs.getGOODS_TYPE());
				map.put("GOODS_BRAND", gs.getGOODS_BRAND());
				map.put("GOODS_SHELVE", gs.getGOODS_SHELVE());
				map.put("GOODS_PRICE", gs.getGOODS_PRICE());
				map.put("GOODS_ARRIVE_DATE", gs.getGOODS_ARRIVE_DATE());
				map.put("GOODS_BAR_CODE", gs.getGOODS_BAR_CODE());
				map.put("GOODS_ORDER", gs.getGOODS_ORDER());
				map.put("GOODS_IMAGE", gs.getGOODS_IMAGE());
				update("saveGoods", map);
			}
			daoManager.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		} finally{
			daoManager.endTransaction();
		}
		
		return "";
	}

}
